// QtCore
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QCoreApplication>
// Локальные
#include "log.h"
#include "settings.h"
#include "solver_tpw.h"

#if !defined(QT_NO_DEBUG)
void HackedMessageOutput(QtMsgType msg_type, const char *msg)
{
    switch(msg_type)
    {
    case QtFatalMsg:    qFatal(msg);    break;
    case QtDebugMsg:    qDebug(msg);    break;
//    case QtWarningMsg:  qWarning(msg);  break;
    case QtCriticalMsg: qCritical(msg); break;
    }
}
#endif

//================================================================================================================================
//================================================================================================================================
int main(int argc, char *argv[])
{
#if !defined(QT_NO_DEBUG)
    qInstallMsgHandler(HackedMessageOutput);
#endif

    QCoreApplication app(argc, argv);
    app.setOrganizationName("BlackSoft"); // :)
    app.setApplicationName("CylinderTPW");
    app.setApplicationVersion("1.0.0");

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

    //****************************************************************************************************************************
    //****************************************************************************************************************************
    QTextStream cout(stdout);
    QTextStream cin(stdin);
    cout.setCodec(QTextCodec::codecForName("IBM866"));
    cin.setCodec(QTextCodec::codecForName("IBM866"));

    Log::init(300000, qApp->applicationDirPath() + "log.txt");

    Settings::init();

    if(!Settings::instance()->exist("AppVersion"))
    {
        //************************************************************************************************************************
        // Настройки программы.
        //************************************************************************************************************************
        Settings::instance()->setSetting("AppVersion", qApp->applicationVersion());
        Settings::instance()->setSetting("OutputPath", "./output");

        //************************************************************************************************************************
        //************************************************************************************************************************
        Settings::instance()->setSetting("Physics/MaxTemperature", 300+82.45);
        Settings::instance()->setSetting("Physics/MinTemperature", 300);

        //************************************************************************************************************************
        //************************************************************************************************************************
        Settings::instance()->setSetting("Math/SlaeIters", 1000);
        Settings::instance()->setSetting("Math/SlaeResidual", 1e-10);
        Settings::instance()->setSetting("Math/NonlinIters", 500);
        Settings::instance()->setSetting("Math/NonlinResidual", 1e-7);
        Settings::instance()->setSetting("Math/DiffStep", 0.000625);
        Settings::instance()->setSetting("Math/RelaxFactor", 0.3);

        //************************************************************************************************************************
        //************************************************************************************************************************
        Settings::instance()->setSetting("Liquid/Density", 155.85);
        Settings::instance()->setSetting("Liquid/Viscosity", 0.0002542455);
        Settings::instance()->setSetting("Liquid/HeatCapacity", 1.);
        Settings::instance()->setSetting("Liquid/Expansiveness", 0.00064);
        Settings::instance()->setSetting("Liquid/HeatConductivity", 0.0583);
    }
    if(Settings::instance()->setting("AppVersion").toString() != qApp->applicationVersion())
    {
    }

    Settings::instance()->sync();

    SolverTPW solver;
    solver.solve();

    cout << QObject::tr("Работа программы успешно завершена!") << endl;
    Settings::deinit();
    Log::deinit();

    return 0;
}
